#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=1010;
int n,L,B,f[MAXN][MAXN],ans=-1;
struct node
{
	int s,t,w,v;
	bool operator<(const node b)const
	{
		return s<b.s;
	}
}a[MAXN*10];
int main()
{
	memset(f,-1,sizeof(f));
	L=read();n=read();B=read();
	f[0][0]=0;
	for(int i=1;i<=n;++i)
	{
		a[i].s=read();a[i].t=read();a[i].v=read();a[i].w=read();
		a[i].t+=a[i].s;
	}
	sort(a+1,a+1+n);
	for(int i=1;i<=n;++i)
		for(int j=B;j>=a[i].w;--j)
			if(~f[a[i].s][j-a[i].w])
				f[a[i].t][j]=max(f[a[i].t][j],f[a[i].s][j-a[i].w]+a[i].v);
	for(int i=1;i<=B;++i)
		ans=max(ans,f[L][i]);
	printf("%d\n",ans);
	return 0;
}
